
package com.dog.basics.service.sys.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.vo.admin.sys.MenuTree;
import com.dog.basics.bean.model.sys.SysMenu;
import com.dog.basics.bean.model.sys.SysTenant;
import com.dog.basics.common.constant.CacheConstants;
import com.dog.basics.common.constant.CommonConstants;
import com.dog.basics.dao.sys.SysRoleMenuMapper;
import com.dog.basics.dao.sys.SysTenantMapper;
import com.dog.basics.service.sys.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户
 *
 * @author guonh
 * @date 2019-05-15 15:55:41
 */
@Service
@AllArgsConstructor
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements SysTenantService {

    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    private final SysDeptRelationService deptRelationService;

    private final SysUserRoleService userRoleService;

    private final SysRoleMenuMapper roleMenuMapper;

    private final SysDictItemService dictItemService;


    private final SysUserService userService;

    private final SysRoleService roleService;

    private final SysMenuService menuService;

    private final SysDeptService deptService;

    private final SysDictService dictService;

    /**
     * 获取正常状态租户
     * <p>
     * 1. 状态正常 2. 开始时间小于等于当前时间 3. 结束时间大于等于当前时间
     *
     * @return
     */
    @Override
    @Cacheable(value = CacheConstants.TENANT_DETAILS)
    public List<SysTenant> getNormalTenant() {
        return baseMapper
                .selectList(Wrappers.<SysTenant>lambdaQuery().eq(SysTenant::getStatus, CommonConstants.STATUS_NORMAL));
    }

    /**
     * 保存租户
     * <p>
     * 1. 保存租户 2. 初始化权限相关表 - sys_user - sys_role - sys_menu - sys_user_role -
     * sys_role_menu - sys_dict - sys_dict_item - sys_client_details - sys_public_params
     *
     * @param sysTenant 租户实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = CacheConstants.TENANT_DETAILS)
    public Boolean saveTenant(SysTenant sysTenant) {
        return this.save(sysTenant);
    }

    /**
     * 保存新的租户菜单，维护成新的菜单
     *
     * @param nodeList 节点树
     * @param parent   上级
     */
    private void saveTenantMenu(List<MenuTree> nodeList, Long parent) {
        for (MenuTree node : nodeList) {
            SysMenu menu = new SysMenu();
            BeanUtils.copyProperties(node, menu, "parentId");
            menu.setParentId(parent);
            menuService.save(menu);
            if (CollUtil.isNotEmpty(node.getChildren())) {
                List<MenuTree> childrenList = node.getChildren().stream().map(treeNode -> (MenuTree) treeNode)
                        .collect(Collectors.toList());
                saveTenantMenu(childrenList, menu.getMenuId());
            }
        }
    }

}
